from collections.abc import Iterable


class Counter(object):
    def __init__(self):
        self.num = 0

    def __iter__(self):
        return self

    def __next__(self):
        self.num += 1
        if self.num > 10:
            raise StopIteration("计数器最多只能到10")
        return self.num

if __name__ == "__main__":
    print(isinstance(123, Iterable))
    print(isinstance(123, int))
    print(isinstance("123", int))
    print(isinstance("123", (int, str)))

    li = [1, 2, 3, 4, 5]
    # 1. 创建迭代器对象
    li_iterator1 = iter(li)
    li_iterator2 = li.__iter__()
    print(li_iterator1)
    print(li_iterator2)
    print(next(li_iterator1))
    print(next(li_iterator1))
    print(next(li_iterator1))
    print(next(li_iterator1))
    print(next(li_iterator1))
    print(next(li_iterator2))
    print(li_iterator2.__next__())
    print(dir(li_iterator1))

    # 2. 测试自定义迭代器类
    counter = Counter()
    for i in counter:
        print(i)